package com.liao.rent.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liao.rent.entity.Feedback;
import com.liao.rent.entity.JsonResult;
import com.liao.rent.entity.SysUserDetails;
import com.liao.rent.service.FeedbackService;
import com.liao.rent.utils.ResultTool;
import com.liao.rent.utils.WebSecurityUtils;
import com.liao.rent.vo.FeedBackReplyVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 反馈表 前端控制器
 * </p>
 *
 * @author liao
 * @since 2024-12-07
 */
@RestController
@Tag(name = "反馈管理")
@RequestMapping("/feedback")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    @Operation(summary = "列表查询")
    @GetMapping("/list")
    public JsonResult<Page<Feedback>> list(int pageNum, int pageSize) {
        LambdaUpdateWrapper<Feedback> LambdaUpdateWrapper = new LambdaUpdateWrapper<>();

        //获取当前用户对象
        SysUserDetails user = WebSecurityUtils.getUser();


        //根据相应的权限查询
        if ( WebSecurityUtils.isLandlord()) {
            LambdaUpdateWrapper.eq(Feedback::getUserId, user.getUser().getId());

        }

        if ( WebSecurityUtils.isRegularUsers()) {
            LambdaUpdateWrapper.eq(Feedback::getUserId, user.getUser().getId());

        }

//                    //获取订单对象数据
        Page<Feedback> page = feedbackService.page(new Page<>(pageNum, pageSize),LambdaUpdateWrapper);
        return ResultTool.success(page);
    }

    @Operation(summary = "通过id对反馈进行回复")
    @PostMapping("/reply")
    @PreAuthorize("hasAuthority('admin')")
    public JsonResult<String> reply(@RequestBody FeedBackReplyVo feedBackReplyVo) {
        try {
            LambdaUpdateWrapper<Feedback> feedbackLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            feedbackLambdaUpdateWrapper.eq(Feedback::getFeedbackId, feedBackReplyVo.getFeedbackId());
            feedbackLambdaUpdateWrapper.set(Feedback::getReply, feedBackReplyVo.getReply());
            //设置反馈状态为已经处理
            feedbackLambdaUpdateWrapper.set(Feedback::getStatus, 1);
            feedbackService.update(feedbackLambdaUpdateWrapper);
        } catch (Exception e) {
            return ResultTool.fail("回复失败");
        }

        return ResultTool.success("回复成功");
    }

    @Operation(summary = "通过id删除反馈信息")
    @PostMapping("/delete/{id}")
    public JsonResult<String> delete(@PathVariable("id") Integer id) {
        try {
            feedbackService.removeById(id);
        } catch (Exception e) {
            return ResultTool.fail("删除失败");
        }
        return ResultTool.success("删除成功");
    }

    @Operation(summary = "添加反馈信息")
    @PostMapping("/add")
    public JsonResult<String> add(@RequestBody Feedback feedback) {
        feedback.setStatus(0);
//        获取当前用户对象
        SysUserDetails user = WebSecurityUtils.getUser();

        feedback.setUserId(user.getUser().getId());

        try {
            feedbackService.save(feedback);
        } catch (Exception e) {
            return ResultTool.fail("添加失败");
        }
        return ResultTool.success("添加成功");


    }
}
